package com.xqboss.web.controller.hama;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xqboss.apps.domain.hama.HamaHelp;
import com.xqboss.apps.dto.hama.*;
import com.xqboss.apps.enums.hama.HamaConfigNumberConfigTypeEnum;
import com.xqboss.apps.service.hama.HamaUserMixService;
import com.xqboss.apps.vo.hama.*;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.domain.PageResult;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.xqboss.common.core.controller.BaseController;
import io.swagger.annotations.Api;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 运动场 - WEB
 * </p>
 *
 * @author 木鱼
 * @since 2024-02-28
 */
@RestController
@Slf4j
@Validated
@Api(tags = "运动场")
@RequestMapping("/web/sport")
@RequiredArgsConstructor
public class WebHamaUserMixController extends BaseController {

    private final HamaUserMixService hamaUserMixService;

    @PreAuthorize("@ss.hasPermi('web:sport:mix:config')")
    @ApiOperation("运动场投装备配置-列表（分页）")
    @GetMapping("mix-config")
    public PageResult<WebHamaMixConfigVo> mixConfig(@Validated WebHamaMixConfigDto<WebHamaMixConfigVo> dto){
        try{
            IPage<WebHamaMixConfigVo> iPage = this.hamaUserMixService.webMixConfig(dto);
            return PageResult.ok(iPage,"获取运动场投装备配置分页列表成功！");
        }catch (Exception e){
            log.error("[mixConfig]获取运动场投装备配置分页列表失败：",e);
            return PageResult.fail(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:mix:state')")
    @Log(title = "运动场投装备配置-禁用/启用", businessType = BusinessType.UPDATE)
    @ApiOperation("运动场投装备配置-禁用/启用")
    @GetMapping("mix-state")
    public R<String> mixEnable(@RequestParam(name = "configId")@ApiParam(name = "configId",value = "配置id",required = true) @NotNull(message = "运动场投装备的配置id不能为空~") Long configId,
                               @RequestParam(name = "state")@ApiParam(name = "state",value = "（1-启用/2-禁用）",required = true) @NotNull(message = "启用/禁用状态不能为空（1-启用/2-禁用）~")@Min(value = 1,message = "1-启用")@Max(value = 2,message = "2-禁用") Integer state){
        try{
            this.hamaUserMixService.mixEnable(configId,state);
            return R.ok_msg(String.format("%s成功~",state==1?"启用":"禁用"));
        }catch (Exception e){
            log.error("[mixEnable]运动场投装备配置-禁用/启用失败：",e);
            return R.fail(String.format("%s失败~",state==1?"启用":"禁用"));
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:mix:info')")
    @ApiOperation("运动场投装备配置-信息")
    @GetMapping("mix-info/{configId}")
    public R<WebHamaMixConfigVo> mixInfo(@PathVariable @ApiParam(name = "configId",value = "配置id",required = true) @NotNull(message = "配置id不能为空~") Long configId){
        try{
            WebHamaMixConfigVo vo = this.hamaUserMixService.mixInfo(configId);
            return R.ok(vo,"运动场投装备配置信息获取成功~");
        }catch (Exception e){
            log.error("[mixInfo]运动场投装备配置信息获取失败：",e);
            return R.fail("运动场投装备配置信息获取失败~");
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:mix:addOrEdit')")
    @Log(title = "运动场投装备配置-新增/编辑", businessType = BusinessType.INSERT_OR_UPDATE)
    @ApiOperation("运动场投装备配置-新增/编辑（携带配置id）")
    @PostMapping("mix-addOrEdit")
    public R<String> mixUpdate(@RequestBody @Validated WebHamaMixConfigDto2 dto2){
        try{
            this.hamaUserMixService.mixUpdate(dto2);
            return R.ok_msg(String.format("运动场投装备配置%s成功~",dto2.getId()==null?"新增":"编辑"));
        }catch (Exception e){
            log.error("[mixUpdate]运动场投装备配置-新增/编辑失败：",e);
            return R.fail(String.format("运动场投装备配置%s失败~",dto2.getId()==null?"新增":"编辑"));
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:mix:basic:speed')")
    @ApiOperation("运动场基础速率数据（投放装备）")
    @GetMapping("basic-speed-mix")
    public R<List<HamaConfigNumberVo>> basicSpeedDataMix(){
        try{
            List<HamaConfigNumberVo> vos =  this.hamaUserMixService.basicSpeedData(HamaConfigNumberConfigTypeEnum.ONE);
            return R.ok(vos,"基础速率（投放装备）配置成功~");
        }catch (Exception e){
            log.error("[basicSpeedDataMix]基础速率（投放装备）配置失败：",e);
            return R.fail("基础速率（投放装备）配置失败~");
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:team:basic:speed')")
    @ApiOperation("运动场基础速率数据（助力）")
    @GetMapping("basic-speed-team")
    public R<HamaConfigNumberTeamVo> basicSpeedDataTeam(){
        try{
            HamaConfigNumberTeamVo vo =  this.hamaUserMixService.basicSpeedDataTeam();
            return R.ok(vo,"基础速率（助力）配置成功~");
        }catch (Exception e){
            log.error("[basicSpeedDataTeam]基础速率（助力）配置失败：",e);
            return R.fail("基础速率（助力）配置失败~");
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:mix:basic:speed:update')")
    @Log(title = "运动场基础速率配置（投放装备）", businessType = BusinessType.INSERT_OR_UPDATE)
    @ApiOperation("运动场基础速率配置（投放装备）")
    @PostMapping("basic-speed-mix-update")
    public R<String> basicSpeedMixUpdate(@RequestBody @Validated List<HamaConfigNumberDto> dto){
        try{
            this.hamaUserMixService.basicSpeedUpdate(dto);
            return R.ok_msg("基础速率（投放装备）配置成功~");
        }catch (Exception e){
            log.error("[basicSpeedMixUpdate]基础速率（投放装备）配置失败：",e);
            return R.fail(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:team:basic:speed:update')")
    @Log(title = "运动场基础速率配置（助力）", businessType = BusinessType.INSERT_OR_UPDATE)
    @ApiOperation("运动场基础速率配置（助力）")
    @PostMapping("basic-speed-team-update")
    public R<String> basicSpeedTeamUpdate(@RequestBody @Validated HamaConfigNumberTeamDto dto){
        try{
            this.hamaUserMixService.basicSpeedTeamUpdate(dto);
            return R.ok_msg("基础速率（助力）配置成功~");
        }catch (Exception e){
            log.error("[basicSpeedTeamUpdate]基础速率（助力）配置失败：",e);
            return R.fail(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:basic:speed:delete')")
    @Log(title = "运动场基础速率配置移除", businessType = BusinessType.DELETE)
    @ApiOperation("运动场基础速率配置移除")
    @GetMapping("basic-speed-delete/{configId}")
    public R<String> basicSpeedDelete(@PathVariable  @ApiParam(name = "configId",value = "配置id",required = true) @NotNull(message = "配置id不能为空~")  Long configId){
        try{
            this.hamaUserMixService.basicSpeedDelete(configId);
            return R.ok_msg("运动场基础速率配置移除成功~");
        }catch (Exception e){
            log.error("[basicSpeedTeamUpdate]运动场基础速率配置移除失败：",e);
            return R.fail("运动场基础速率配置移除失败~");
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:team:admins')")
    @ApiOperation("队伍队长列表")
    @GetMapping("team-admins")
    public PageResult<HamaHelpAdminVO> teamAdmins(HamaHelpAdminDto<HamaHelpAdminVO> dto){
        try{
            IPage<HamaHelpAdminVO> iPage = this.hamaUserMixService.selectTeamAdmins(dto);
            return PageResult.ok(iPage,"队伍队长列表获取成功~");
        }catch (Exception e){
            log.error("[teamAdmins]队伍队长列表获取失败：",e);
            return PageResult.fail("队伍队长列表获取失败~");
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:team:members')")
    @ApiOperation("队伍成员详情")
    @GetMapping("team-members")
    public PageResult<HamaHelpMemberVO> teamMembers(HamaHelpMemberDto<HamaHelp> dto){
        try{
            IPage<HamaHelpMemberVO> iPage = this.hamaUserMixService.selectTeamMembers(dto);
            return PageResult.ok(iPage,"队伍成员详情获取成功~");
        }catch (Exception e){
            log.error("[teamMembers]队伍成员详情获取失败：",e);
            return PageResult.fail("队伍成员详情获取失败~");
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:build:list')")
    @ApiOperation("运动场建设-列表")
    @GetMapping("build-list")
    public PageResult<WebHamaOpenConfigVo> buildList(HamaBuildDto<HamaHelpAdminVO> dto){
        try{
            IPage<WebHamaOpenConfigVo> iPage = this.hamaUserMixService.selectBuildList(dto);
            return PageResult.ok(iPage,"运动场建设列表获取成功~");
        }catch (Exception e){
            log.error("[buildList]运动场建设列表获取失败：",e);
            return PageResult.fail("运动场建设列表获取失败~");
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:build:info')")
    @ApiOperation("运动场建设-信息")
    @GetMapping("build-info/{configId}")
    public R<WebHamaOpenConfigVo> buildInfo(@PathVariable  @ApiParam(name = "configId",value = "配置id",required = true) @NotNull(message = "配置id不能为空~")  Long configId){
        try{
            WebHamaOpenConfigVo vo = this.hamaUserMixService.selectBuildInfo(configId);
            return R.ok(vo,"运动场建设信息获取成功~");
        }catch (Exception e){
            log.error("[buildInfo]运动场建设信息获取失败：",e);
            return R.fail("运动场建设信息获取失败~");
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:build:state')")
    @Log(title = "运动场建设-禁用/启用", businessType = BusinessType.UPDATE)
    @ApiOperation("运动场建设-禁用/启用")
    @GetMapping("build-state")
    public R<String> buildEnable(@RequestParam(name = "configId")@ApiParam(name = "configId",value = "配置id",required = true) @NotNull(message = "运动场建设的配置id不能为空~") Long configId,
                               @RequestParam(name = "state")@ApiParam(name = "state",value = "（1-启用/2-禁用）",required = true) @NotNull(message = "启用/禁用状态不能为空（1-启用/2-禁用）~")@Min(value = 1,message = "1-启用")@Max(value = 2,message = "2-禁用") Integer state){
        try{
            this.hamaUserMixService.buildEnable(configId,state);
            return R.ok_msg(String.format("%s成功~",state==1?"启用":"禁用"));
        }catch (Exception e){
            log.error("[buildEnable]运动场建设配置-禁用/启用失败：",e);
            return R.fail(String.format("%s失败~",state==1?"启用":"禁用"));
        }
    }

    @PreAuthorize("@ss.hasPermi('web:sport:build:addOrEdit')")
    @Log(title = "运动场建设-新增/编辑", businessType = BusinessType.INSERT_OR_UPDATE)
    @ApiOperation("运动场建设-新增/编辑（携带配置id）")
    @PostMapping("build-addOrEdit")
    public R<String> buildUpdate(@RequestBody @Validated WebHamaOpenConfigDto dto){
        try{
            this.hamaUserMixService.buildUpdate(dto);
            return R.ok_msg(String.format("运动场建设配置%s成功~",dto.getId()==null?"新增":"编辑"));
        }catch (Exception e){
            log.error("[buildUpdate]运动场建设配置-新增/编辑失败：",e);
            return R.fail(String.format("运动场建设配置%s失败~",dto .getId()==null?"新增":"编辑"));
        }
    }

}
